package allen.ui.log;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import allen.log.LogDataCenter;
import allen.log.LogLevel;

/**
 * Log setting panel.
 * */
class LogSettingPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private static LogDataCenter dc = LogDataCenter.logDataCenter;

	private JLabel maxCapacityLabel = new JLabel("Max Capacity",
			SwingConstants.CENTER);
	private JComboBox maxCapacityComboBox;

	private JLabel minLogLevelLabel = new JLabel("Min Log Level",
			SwingConstants.CENTER);
	private JComboBox minLogLevelComboBox;

	private JLabel switchLabel = new JLabel("", SwingConstants.CENTER);
	private JButton switchButton = new JButton();

	private JLabel clearLabel = new JLabel("Clear log", SwingConstants.CENTER);
	private JButton clearButton = new JButton("Clear");

	/**
	 * Update log center's switch status's related text.
	 * */
	private void updateSwitchText() {
		if (dc.isLogOn()) {
			switchLabel.setForeground(Color.BLUE);
			switchLabel.setText("Log is On");
			switchButton.setText("Turn Off");
		} else {
			switchLabel.setForeground(Color.GRAY);
			switchLabel.setText("Log is Off");
			switchButton.setText("Turn On");
		}
	}

	private void initMaxCapacityComboBox() {
		maxCapacityComboBox = new JComboBox(new Integer[] { 10, 50, 100, 200,
				500, 1000 });
		maxCapacityComboBox.setMaximumSize(LogConstant.DefaultComboSize);
		maxCapacityComboBox.setSelectedItem(LogDataCenter.Default_Max_Capacity);
	}

	private void initMinLogLevelComboBox() {
		minLogLevelComboBox = new JComboBox(LogLevel.values());
		minLogLevelComboBox.setMaximumSize(LogConstant.DefaultComboSize);
		minLogLevelComboBox.setSelectedItem(LogDataCenter.Default_Min_LogLevel);
	}

	private void init() {

		this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		this.setBorder(BorderFactory
				.createTitledBorder(LogConstant.SettingTitle));

		maxCapacityLabel.setPreferredSize(LogConstant.DefaultLabelSize);
		this.add(maxCapacityLabel);

		initMaxCapacityComboBox();
		this.add(maxCapacityComboBox);

		minLogLevelLabel.setPreferredSize(LogConstant.DefaultLabelSize);
		this.add(minLogLevelLabel);

		initMinLogLevelComboBox();
		this.add(minLogLevelComboBox);

		switchLabel.setPreferredSize(LogConstant.DefaultLabelSize);
		this.add(switchLabel);

		switchButton.setPreferredSize(LogConstant.DefaultButtonSize);
		this.add(switchButton);

		updateSwitchText();

		this.add(Box.createHorizontalGlue());

		clearLabel.setPreferredSize(LogConstant.DefaultLabelSize);
		this.add(clearLabel);

		clearButton.setPreferredSize(LogConstant.DefaultButtonSize);
		this.add(clearButton);
	}

	private void setToolTipsForUI() {
		maxCapacityLabel.setToolTipText(LogConstant.MaxCapacityToolTip);
		maxCapacityComboBox.setToolTipText(LogConstant.MaxCapacityToolTip);
		minLogLevelLabel.setToolTipText(LogConstant.MinLogLevelToolTip);
		minLogLevelComboBox.setToolTipText(LogConstant.MinLogLevelToolTip);
		clearButton.setToolTipText(LogConstant.ClearToolTip);
	}

	private void addListeners() {
		maxCapacityComboBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int maxCapacity = (Integer) maxCapacityComboBox
						.getSelectedItem();
				dc.setMaxCapacity(maxCapacity);
			}
		});

		minLogLevelComboBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				LogLevel minLogLevel = (LogLevel) minLogLevelComboBox
						.getSelectedItem();
				dc.setMinLogLevel(minLogLevel);
			}
		});
		switchButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dc.turnLog();
				updateSwitchText();
			}
		});
		clearButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dc.clearAllLogEntities();
			}
		});
	}

	LogSettingPanel() {
		init();
		setToolTipsForUI();
		addListeners();
	}
}
